﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Globalization;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using System.Data;
using ncp.Business;
using ncp.Business.Common;

namespace ncp.Web.cms
{
    public partial class Client_List : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            AjaxPro.Utility.RegisterTypeForAjax(typeof(Client_List));
        }

        //得到总记录数
        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public int GetRecordCount(string currentwhere)
        {
            return ClientInfoBll.GetClientInfoCount(currentwhere, CommonConst.DB_Name);
        }

        //装载商品表视图
        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public string BindTable(string jsonData)
        {
            Dictionary<string, object> DictList = JsonHelper.JSONToObject<Dictionary<string, object>>(jsonData);
            DataTable dt = ClientInfoBll.GetClientInfoTable(int.Parse(DictList["currentpageindex"].ToString()) - 1, int.Parse(DictList["pagesize"].ToString()), "*", DictList["sorting"].ToString() + " " + DictList["sortdir"].ToString(), DictList["currentwhere"].ToString(), CommonConst.DB_Name);
            return  JsonHelper.DataTableToJSON(dt);
        }

        //得到初始条件
        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public string GetstrWhere(string pQueryName, string pQueryOperator, string pQueryValue)
        {
            string myWhere = "1=1";
            if (pQueryValue != "")
            {
                if (pQueryOperator == "like")
                {
                    myWhere += " and " + pQueryName + " like '%" + pQueryValue + "%'";
                }
                else
                {
                    myWhere += " and " + pQueryName + " " + pQueryOperator + " '" + pQueryValue + "'";
                }
            }
            return myWhere;
        }

        //客户升级为VIP
        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public string UpdateClientVip(string jsonData)
        {
            Dictionary<string, object> jsonResult = new Dictionary<string, object>();
            Dictionary<string, object> DictList = JsonHelper.JSONToObject<Dictionary<string, object>>(jsonData);
            try
            {
                ClientInfoBll.UpdateClientVip(DictList, CommonConst.DB_Name);
                var userInfo = WebMasterCookie.GetCookie(CommonConst.adminCookieName);
                Dictionary<string, object> dicHandleLog = new Dictionary<string, object>();
                dicHandleLog.Add("user_id", userInfo.Id);
                dicHandleLog.Add("computername", userInfo.Computername);
                dicHandleLog.Add("computerip", userInfo.Computerip);
                dicHandleLog.Add("handle_note", "客户升级为VIP");
                dicHandleLog.Add("obj_guid", "Client_Id=" + DictList["client_id"].ToString());
                LogHandleBll.AddHandleLog(dicHandleLog, CommonConst.DB_Name);
                jsonResult.Add("msg", "客户升级为VIP成功！");
                jsonResult.Add("isreload", true);
            }
            catch (Exception ex)
            {
                jsonResult.Clear();
                jsonResult.Add("msg", ex.Message);
                jsonResult.Add("isreload", false);
            }
            return "[" + JsonHelper.ObjectToJSON(jsonResult) + "]";
        }

        //设置客户
        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public string UpdateClientStatus(int Client_Id, int User_Status)
        {
            Dictionary<string, object> jsonResult = new Dictionary<string, object>();
            try
            {
                int ii = ClientInfoBll.UpdateClientStatus(Client_Id.ToString(), User_Status.ToString(), CommonConst.DB_Name);
                if (ii == 0)
                {
                    jsonResult.Add("msg", "设置不成功！");
                    jsonResult.Add("isreload", false);
                }
                else
                {
                    var userInfo = WebMasterCookie.GetCookie(CommonConst.adminCookieName);
                    Dictionary<string, object> dicHandleLog = new Dictionary<string, object>();
                    dicHandleLog.Add("user_id", userInfo.Id);
                    dicHandleLog.Add("computername", userInfo.Computername);
                    dicHandleLog.Add("computerip", userInfo.Computerip);
                    dicHandleLog.Add("handle_note", (User_Status == 0 ? "禁用客户" : "启动客户"));
                    dicHandleLog.Add("obj_guid", "Client_Id=" + Client_Id.ToString());
                    LogHandleBll.AddHandleLog(dicHandleLog, CommonConst.DB_Name);
                    jsonResult.Add("msg", "设置成功！");
                    jsonResult.Add("isreload", true);
                }
            }
            catch (Exception ex)
            {
                jsonResult.Clear();
                jsonResult.Add("msg", ex.Message);
                jsonResult.Add("isreload", false);
            }
            return "[" + JsonHelper.ObjectToJSON(jsonResult) + "]";
        }

        //设置自定义属性
        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public string SetFlags(int Client_Id, string flags)
        {
            Dictionary<string, object> jsonResult = new Dictionary<string, object>();
            try
            {
                ClientInfoBll.UpateUser_Flag(Client_Id.ToString(), flags, CommonConst.DB_Name);
                var userInfo = WebMasterCookie.GetCookie(CommonConst.adminCookieName);
                Dictionary<string, object> dicHandleLog = new Dictionary<string, object>();
                dicHandleLog.Add("user_id", userInfo.Id);
                dicHandleLog.Add("computername", userInfo.Computername);
                dicHandleLog.Add("computerip", userInfo.Computerip);
                dicHandleLog.Add("handle_note", "设置客户发布属性");
                dicHandleLog.Add("obj_guid", "Client_Id=" + Client_Id.ToString() + "(" + flags + ")");
                LogHandleBll.AddHandleLog(dicHandleLog, CommonConst.DB_Name);

                jsonResult.Add("isreload", true);
                jsonResult.Add("msg", "自定义属性设置成功!");
            }
            catch (Exception ex)
            {
                jsonResult.Clear();
                jsonResult.Add("msg", ex.Message);
                jsonResult.Add("isreload", false);
            }
            return "[" + JsonHelper.ObjectToJSON(jsonResult) + "]";
        }
    }
}
